<?php
  require_once("../includes/config.php"); 
  
    //make sure the person in authorized
  if ($_SESSION["r"]>2&&$_SESSION['r']!=4)
  {
  	$_SESSION['error_message'] = "غير مسموح";
	redirect("index.php");
	die();
  }

//Edit Pop-up"
 //dump($_POST);
  if($_SERVER["REQUEST_METHOD"] == "POST")
  {
    $date=date('y-m-d');
    $time=  date('g:ia');
		
     if(isset($_POST['internet']))
        $internet = 1;
      else
        $internet = 0;
     
     if(isset($_POST['park']))
        $park = 1;
      else
        $park = 0;
     
     if(isset($_POST['elevator']))
        $elevator = 1;
      else
        $elevator = 0;
     
     if(isset($_POST['clean']))
        $clean = 1;
      else
        $clean = 0;
     
     if(isset($_POST['phoneguide']))
        $phoneguide = 1;
      else
        $phoneguide = 0;
     
     if(isset($_POST['oven']))
        $oven = 1;
      else
        $oven = 0;
        
     if(isset($_POST['newspapaer']))
        $newspapaer = 1;
      else
        $newspapaer = 0;
        
     if(isset($_POST['microwave']))
        $microwave = 1;
      else
        $microwave = 0;
        
     if(isset($_POST['phoneguide']))
        $phoneguide = 1;
      else
        $phoneguide = 0;
        
     if(isset($_POST['chkWashingMachine']))
        $chkWashingMachine = 1;
      else
        $chkWashingMachine = 0;
        
     if(isset($_POST['resturant']))
        $resturant = 1;
      else
        $resturant = 0;
        
     if(isset($_POST['chkIron']))
        $chkIron = 1;
      else
        $chkIron = 0;
        
     if(isset($_POST['fridge']))
        $fridge = 1;
      else
        $fridge = 0;
        
     if(isset($_POST['chkFoodTable']))
        $chkFoodTable = 1;
      else
        $chkFoodTable = 0;
        
     if(isset($_POST['hall']))
        $hall = 1;
      else
        $hall = 0;
        
     if(isset($_POST['chkKitchen']))
        $chkKitchen = 1;
      else
        $chkKitchen = 0;
        
     if(isset($_POST['qibala']))
        $qibala = 1;
      else
        $qibala = 0; 
    $results = query("SELECT * from apartments where apart_name = ? and owner_id = ? and id != ?",$_POST['name'],$_SESSION['user_id'],$_POST['apart_id']);
  
    if(!empty($results))
    {
      $_SESSION['error_message'] = "اسم الوحده الذى ادخلته موجود مسبقاً";
    }
    else
    {
		if ($_SESSION['r']<=1||$_SESSION['r']==4)
		{
			if($_POST['apart_status']==8&&$_SESSION['r']<=1){
				$status=query('SELECT * FROM apartments WHERE id=?',$_POST['apart_id']);
				$stat=$status[0]['status'];
				$sql= query("INSERT INTO requests (apart_status	,apart_name ,owner_id ,requset_type,time_beginning,date_beginning,time_ending,date_ending) VALUES (?,?,?,?,?,?,?,?)",$stat,$_POST['name'],$_SESSION['user_id'],$_POST['apart_status'],$time,$date,"","");
				$update_status = query('UPDATE apartments SET status = ? WHERE id = ?',$_POST['apart_status'],$_POST['apart_id']);
			} 
			elseif($_POST['status']==100){
				$result=query("SELECT * FROM requests WHERE date_ending=? and apart_name=?","0000-00-00",$_POST['name']);
				$update_status = query('UPDATE apartments SET status = ? WHERE id = ?',$result[0]['apart_status'],$_POST['apart_id']);
				$update_request=query("UPDATE requests SET time_ending=? , date_ending=? WHERE apart_name=?",$time,$date,$_POST['name']);
			}
			elseif($_SESSION['r']<=1)
			{
				$result = query("
				UPDATE apartments SET
				apart_name = ?,apart_desc=? ,floor = ?, rooms = ?, bathrooms = ?,single_beds = ? ,double_beds = ?,safes = ?,TV = ?,cooler_type = ?,room_type = ?,status = ?,internet = ?,park = ?,elevator = ?,clean_service = ?,
				telephone = ?,newspapers = ?,qebla = ?,rest_list = ?,refregrator = ?,hall = ?,oven = ?,microwave = ?,washer = ?,iron = ?,food_table = ?,kitchen = ?,additional_notes = ? WHERE id = ? AND owner_id=?",
				$_POST['name'],$_POST['desc'],$_POST['floor'],$_POST['room_num'],$_POST['bathrooms'],$_POST['single_beds'],$_POST['double_beds'],$_POST['safe'],$_POST['tv'],$_POST['cooler_type'],$_POST['apart_type'],
				$_POST['apart_status'],$internet,$park,$elevator,$clean,$phoneguide,$newspapaer,$qibala,$resturant,$fridge,
				$hall,$oven,$microwave,$chkWashingMachine,$chkIron,$chkFoodTable,$chkKitchen,$_POST['txtComments'],$_POST['apart_id'],$_SESSION['user_id']);
			}
		}
		
		else
		{
		if ($_POST['apart_status']==8)
		{
		$status=query('SELECT * FROM apartments WHERE id=?',$_POST['apart_id']);
		$stat=$status[0]['status'];
		$sql= query("INSERT INTO requests (apart_status	,apart_name ,owner_id ,requset_type,time_beginning,date_beginning,time_ending,date_ending) VALUES (?,?,?,?,?,?,?,?)",$stat,$_POST['name'],$_SESSION['user_id'],$_POST['apart_status'],$time,$date,"","");
		$update_status = query('UPDATE apartments SET status = ? WHERE id = ? AND owner_id=?',$_POST['apart_status'],$_POST['apart_id'],$_SESSION['user_id']);
		}
		else
		{
			$result = query("UPDATE apartments SET status = ? WHERE id = ? AND owner_id=?",$_POST['apart_status'],$_POST['apart_id'],$_SESSION['user_id']);
		}
		}
		$_SESSION['success_message'] = "تم التعديل بنجاح"; 
      
    }
    
     redirect("apartments.php?page='". $_POST["page"]."'");
   

 }
	?>